﻿using System;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    abstract class SerAr
    {

        public static SerAr Read(BinaryReader reader)
        {
            SerAr result = GetSerAr(reader.ReadByte());
            result.ReadContent(reader);
            return result;
        }

        private static SerAr GetSerAr(byte b)
        {
            switch (b)
            {
                case 0:
                    return new SerNil();
                case 0x04:
                    return new SerBool();   
                case 0x10:
                    return new SerErr();
                case 0x01:
                    return new SerNum();
                case 0x02:
                    return new SerStr();
                default: throw new NotSupportedException();
            }
        }

        protected abstract void ReadContent(BinaryReader reader);

        public void Write(BinaryWriter writer)
        {
            throw new NotImplementedException();
        }
    }
    class SerNil: SerAr{
        protected override void ReadContent(BinaryReader reader)
        {
            reader.ReadBytes(8);
        }
    }
    class SerNum : SerAr {
        private double num;

        protected override void ReadContent(BinaryReader reader)
        {
            this.num = reader.ReadDouble();
        }
    }
    class SerStr : SerAr {
        protected override void ReadContent(BinaryReader reader)
        {
            str = new XLUnicodeString();
            str.Read(reader);
        }

        private XLUnicodeString str;
    }
    class SerBool : SerAr {
        private byte f;

        protected override void ReadContent(BinaryReader reader)
        {
            this.f = reader.ReadByte();
            reader.ReadBytes(7);
        }
    }
    class SerErr : SerAr {
        private byte err;

        protected override void ReadContent(BinaryReader reader)
        {
            this.err = reader.ReadByte();
            reader.ReadBytes(7);
        }
    }
}